{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 数据结构\n",
    "\n",
    "\n",
    "- **list**(列表): 支持插入，弹出，索引，切片等\n",
    "\n",
    "\n",
    "- **tuple**(元组): 一经创建不可修改\n",
    "\n",
    "\n",
    "- **set**(集合): 交集，并集等操作\n",
    "\n",
    "\n",
    "- **dict**(字典): 无序的key－val健值对\n",
    "\n",
    "\n",
    "以上四种结构也可以相互嵌套， **list** 和 **dict**是最常用的两个结构。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表创建\n",
    "\n",
    "\n",
    "+ 命名一个列表，如果你列表里都是同学的名字，就叫students\n",
    "+ python使用方括号指明列表，students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "+ range函数的使用\n",
    "+ 枚举法\n",
    "+ 空列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "numbers = list(range(1,6)) \n",
    "print(numbers) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 7]\n"
     ]
    }
   ],
   "source": [
    "numbers = list(range(1,9,3)) \n",
    "print(numbers) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 2, 3, 5, 8, 13, 21]\n"
     ]
    }
   ],
   "source": [
    "fibonacci_numbers = [1, 1, 2, 3, 5, 8, 13, 21]  #斐波那契数\n",
    "print(fibonacci_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['韩梅梅', '李雷', '林涛', 'Jim', 'Kate', 'Lucy']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "我印象最深的同学有6个，他们是：\n",
      "Lucy\n",
      "Kate\n",
      "Jim\n",
      "林涛\n",
      "李雷\n",
      "韩梅梅\n"
     ]
    }
   ],
   "source": [
    "student_count = len(students)\n",
    "print(student_count)\n",
    "print('我印象最深的同学有' + str(student_count) + '个，他们是：' )\n",
    "for student in students:\n",
    "    print(student)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 2, 3, 5, 8, 13, 21]\n",
      "['a', 'b', 'c', 'd']\n",
      "['a', 1, 'b', 2, 'c', 3, True, False]\n",
      "[[1, 1, 2, 3, 5, 8, 13, 21], ['a', 'b', 'c', 'd'], ['a', 1, 'b', 2, 'c', 3, True, False]]\n"
     ]
    }
   ],
   "source": [
    "# list内元素支持不同类型的数据；\n",
    "# R中的vector要求是同类型的，不可混杂\n",
    "num_str_bool = ['a', 1, 'b', 2, 'c', 3, True, False]\n",
    "print(num_str_bool)\n",
    "\n",
    "# list嵌套\n",
    "print([fibonacci_numbers, strings, num_str_bool])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "usernames = []\n",
    "print(usernames)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表解析（列表推导式）\n",
    "\n",
    "+ 一种优雅的生成列表的方式\n",
    "+ 不失可读性\n",
    "+ 性能快"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 2, 3, 5, 8, 13, 21, 999]\n"
     ]
    }
   ],
   "source": [
    "# 给列表后面添加一个值\n",
    "fibonacci_numbers.append(999)\n",
    "print(fibonacci_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]\n"
     ]
    }
   ],
   "source": [
    "# 传统写法\n",
    "a=[]\n",
    "for i in range(101):\n",
    "    if i%2==0:\n",
    "        a.append(i)\n",
    "        \n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]\n"
     ]
    }
   ],
   "source": [
    "# 列表解析\n",
    "a=[x for x in range(101) if x%2==0]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t\n",
      "['M', 'h', 'i', 'f', 'o', 't']\n"
     ]
    }
   ],
   "source": [
    "text=\"My house is full of toys\"\n",
    "first_charts=[]\n",
    "for word in text.split():\n",
    "    first_charts.append(word[0])\n",
    "    \n",
    "print(word[0])\n",
    "print(first_charts)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['M', 'h', 'i', 'f', 'o', 'f']\n"
     ]
    }
   ],
   "source": [
    "# 列表解析\n",
    "first_charts=[word[0] for word in text.split()]\n",
    "print(first_charts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 0, 8]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=['1','2','3','i','8']\n",
    "[int(i) if i.isdigit() else 0 for i in a]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 2, 3, 5, 8, 13, 21]\n",
      "[2, 2, 4, 6, 14, 22]\n"
     ]
    }
   ],
   "source": [
    "fibonacci_numbers = [1, 1, 2, 3, 5, 8, 13, 21]  #斐波那契数\n",
    "print(fibonacci_numbers)\n",
    "print([ i + 1  for i in fibonacci_numbers if i % 2 != 0 ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fibonacci_numbers + list(range(1,10))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*如何用列表解析写出一个和上面一样的结果？*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 常见列表操作\n",
    "\n",
    "+ 读取一个列表的任意一个项目(item)\n",
    "+ 遍历一个列表的每个项目(item)\n",
    "+ 改变list中item的值\n",
    "+ 在列表中找一个item\n",
    "+ 在列表中增加一个item\n",
    "+ 对列表进行排序\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读取一个列表的任意一个项目(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "韩梅梅\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "student = students[0]\n",
    "print(student)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lucy\n"
     ]
    }
   ],
   "source": [
    "student = students[-1]\n",
    "print(student)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 遍历一个列表的每个项目(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "韩梅梅\n",
      "李雷\n",
      "林涛\n",
      "Jim\n",
      "Kate\n",
      "Lucy\n"
     ]
    }
   ],
   "source": [
    "for student in students:\n",
    "    print(student)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我想韩梅梅\n",
      "我想李雷\n",
      "我想林涛\n",
      "我想Jim\n",
      "我想Kate\n",
      "我想Lucy\n",
      "我想......\n",
      "\n",
      "我想你们所有人\n"
     ]
    }
   ],
   "source": [
    "for student in students:\n",
    "    print('我想' + student)\n",
    "\n",
    "print('我想......\\n')\n",
    "print('我想你们所有人')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我想0排的韩梅梅\n",
      "我想1排的李雷\n",
      "我想2排的林涛\n",
      "我想3排的Jim\n",
      "我想4排的Kate\n",
      "我想5排的Lucy\n"
     ]
    }
   ],
   "source": [
    "# enumerate函数的使用，跟踪每个item的序号\n",
    "for index, student in enumerate(students):\n",
    "    place = str(index)\n",
    "    print('我想' + place + '排的' + student)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 改变list中item的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['韩梅梅', '李雷', '林涛', 'Jim', '杨洋', 'Lucy']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "students[4] = '杨洋'\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在列表中找一个item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "'杨洋' is not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-6f400da47645>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mstudents\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'韩梅梅'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'李雷'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'林涛'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'Jim'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'Kate'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'Lucy'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstudents\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'杨洋'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: '杨洋' is not in list"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "print(students.index('杨洋'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "print('杨洋' in students)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在列表中增加item\n",
    "\n",
    "#### 用append增加，不限类型\n",
    "#### 用extend增加，只能增加list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['韩梅梅', '李雷', '林涛', 'Jim', 'Kate', 'Lucy', '杨洋']\n"
     ]
    }
   ],
   "source": [
    "# 后面加\n",
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "students.append('杨洋')\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['韩梅梅', '李雷', '林涛', 'Jim', 'Kate', 'Lucy', '陈粒', ['陈粒', '邵夷贝'], '杨洋', '张伟', 'T', 'a', 'y', 'l', 'o', 'r', '花', '粥', '谢春花']\n"
     ]
    }
   ],
   "source": [
    "# 后面加\n",
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "\n",
    "\n",
    "# append\n",
    "students.append('陈粒')\n",
    "students.append(['陈粒','邵夷贝'])\n",
    "\n",
    "# extend\n",
    "students.extend(['杨洋','张伟'])\n",
    "students.extend('Taylor')\n",
    "students.extend('花粥')\n",
    "students.extend(['谢春花'])\n",
    "\n",
    "\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6]]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=[1,2,3]\n",
    "b=[4,5,6]\n",
    "a.append(b)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=[1,2,3]\n",
    "a.extend(b)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['韩梅梅', '李雷', '杨洋', '林涛', 'Jim', 'Kate', 'Lucy']\n"
     ]
    }
   ],
   "source": [
    "# 指定index加\n",
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "students.insert(2,'杨洋')\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删去一个item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['李雷', '林涛', 'Jim', 'Kate', 'Lucy']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "del students[0]\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['韩梅梅', '李雷', '林涛', 'Jim', 'Lucy']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "students.remove('Kate')\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对列表的pop\n",
    "\n",
    "+ 后进的先用\n",
    "+ 用一个少一个\n",
    "\n",
    "![](http://i1.fuimg.com/611786/8e8cd75b9692c8f0.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lucy\n",
      "['韩梅梅', '李雷', '林涛', 'Jim', 'Kate']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "last_stu = students.pop()\n",
    "\n",
    "print(last_stu)\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "韩梅梅\n",
      "['李雷', '林涛', 'Jim', 'Kate', 'Lucy']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "last_stu = students.pop(0)\n",
    "\n",
    "print(last_stu)\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对列表进行排序\n",
    "\n",
    "sort和sorted的区别：\n",
    "+ 如果用sort对一个列表进行排序，无法再回到原来的顺序。\n",
    "+ 如果想排序显示但是又保留原来的顺序，就使用sorted()函数，而且sorted()也支持逆序*reverse=TRUE*。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jim\n",
      "Kate\n",
      "Lucy\n",
      "李雷\n",
      "林涛\n",
      "韩梅梅\n",
      "['Jim', 'Kate', 'Lucy', '李雷', '林涛', '韩梅梅']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "students.sort()\n",
    "for student in students:\n",
    "    print(student)\n",
    "    \n",
    "print(students)\n",
    "\n",
    "# python对中文排序比较乱，需要另行处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jim\n",
      "Kate\n",
      "Lucy\n",
      "李雷\n",
      "林涛\n",
      "韩梅梅\n",
      "['韩梅梅', '李雷', '林涛', 'Jim', 'Kate', 'Lucy']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "for student in sorted(students, reverse=False):\n",
    "    print(student)\n",
    "    \n",
    "print(students)\n",
    "# 可逆的排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Lucy', 'Kate', 'Jim', '林涛', '李雷', '韩梅梅']\n"
     ]
    }
   ],
   "source": [
    "# 直接反转列表顺序\n",
    "\n",
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "students.reverse()\n",
    "\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numbers = [1, 3, 4, 2]\n",
    "\n",
    "* 如何对numbers排序并且不改变原顺序？*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 索引和切片\n",
    "\n",
    "Python的索引和切片都是**从0开始**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 2, 3, 5, 8, 13, 21]\n",
      "第1个元素: 1\n",
      "第4个元素: 3\n",
      "前3个元素: [1, 1, 2]\n",
      "第4到最后: [3, 5, 8, 13, 21]\n",
      "前3个数: [1, 1, 2]\n"
     ]
    }
   ],
   "source": [
    "fibonacci_numbers = [1, 1, 2, 3, 5, 8, 13, 21] \n",
    "print(fibonacci_numbers)\n",
    "\n",
    "# 从0开始\n",
    "print('第1个元素:', fibonacci_numbers[0]) # the first element\n",
    "print('第4个元素:', fibonacci_numbers[3]) # the fourth element\n",
    "\n",
    "# 包含左侧，不包含右侧 \n",
    "print('前3个元素:', fibonacci_numbers[0:3]) # 前三个元素 [0, 3)\n",
    "print('第4到最后:', fibonacci_numbers[3:]) # 第4个到最后\n",
    "print('前3个数:', fibonacci_numbers[:3]) #  前3个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['林涛', 'Jim', 'Kate']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "f4 = students[2:5]\n",
    "print(f4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 复制列表\n",
    "+ 注意使用“=”和copy()的区别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 2, 3, 5, 8, 13, 21]\n",
      "[1, 1, 2, 3, 5, 8, 13, 21]\n",
      "[1, 1, 2, 3, 5, 8, 13, 21, 9999]\n",
      "[1, 1, 2, 3, 5, 8, 13, 21, 9999]\n",
      "[1, 1, 2, 3, 5, 8, 13, 21]\n"
     ]
    }
   ],
   "source": [
    "fibonacci_numbers = [1, 1, 2, 3, 5, 8, 13, 21]  #斐波那契数\n",
    "aa = fibonacci_numbers\n",
    "print(aa)\n",
    "\n",
    "bb = fibonacci_numbers.copy()\n",
    "print(bb)\n",
    "\n",
    "fibonacci_numbers.append(9999)\n",
    "print(fibonacci_numbers)\n",
    "print(aa)                       # 等号的话，值会传递\n",
    "print(bb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 元组 \n",
    "+ 元组使用()创立，列表使用[]创立"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "age\n",
      "30\n"
     ]
    }
   ],
   "source": [
    "# 元组 tuple /taple, tjuple/\n",
    "tuple_example = ('age', 30) # 只读，有序\n",
    "print(type(tuple_example))\n",
    "print(tuple_example[0])\n",
    "print(tuple_example[1])\n",
    "# tuple不能修改元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The first color is: red\n",
      "\n",
      "The available colors are:\n",
      "- red\n",
      "- green\n",
      "- blue\n"
     ]
    }
   ],
   "source": [
    "colors = ('red', 'green', 'blue')\n",
    "print(\"The first color is: \" + colors[0])\n",
    "\n",
    "print(\"\\nThe available colors are:\")\n",
    "for color in colors:\n",
    "    print(\"- \" + color)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'tuple' object has no attribute 'append'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-78-ed1dbff53ab2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mcolors\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'red'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'green'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'blue'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mcolors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'purple'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
     ]
    }
   ],
   "source": [
    "colors = ('red', 'green', 'blue')\n",
    "colors.append('purple')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('purple', 'green', 'pink')\n"
     ]
    }
   ],
   "source": [
    "colors = ('purple', 'green', 'pink')\n",
    "print(colors)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
